import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { CmtActivityInfoApi } from '#/api/cmt/activity/info';

import { CommonStatusEnum, DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { z } from '#/adapter/form';
import { getRangePickerDefaultProps } from '#/utils';
import { getBaseCommunityOptions } from '#/api/cmt/base/community';
import { getMerchantInfoOptions } from '#/api/cmt/base/merchantInfo';
import { getActivityCategoryOptions } from '#/api/cmt/activity/category';
import { getVenueInfoOptions } from '#/api/cmt/venue/info';

/** 活动状态选项 */
export const ACTIVITY_STATUS_OPTIONS = [
  { label: '草稿', value: 0 },
  { label: '报名中', value: 1 },
  { label: '进行中', value: 2 },
  { label: '已结束', value: 3 },
  { label: '已取消', value: 4 },
];

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'communityId',
      label: '社区',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择社区',
        api: getBaseCommunityOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'merchantId',
      label: '商户',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择活动发起商户',
        api: getMerchantInfoOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'categoryId',
      label: '活动分类',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择活动分类',
        api: getActivityCategoryOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'title',
      label: '活动标题',
      rules: 'required',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入活动标题',
      },
    },
    {
      fieldName: 'coverImage',
      label: '封面图片',
      component: 'ImageUpload',
      formItemClass: 'col-span-1',
    },
    {
      fieldName: 'bannerImage',
      label: '轮播图片',
      component: 'ImageUpload',
      formItemClass: 'col-span-1',
    },
    {
      fieldName: 'images',
      label: '活动图片',
      component: 'ImageUpload',
      formItemClass: 'col-span-2',
      componentProps: {
        maxCount: 9,
      },
    },
    {
      fieldName: 'description',
      label: '活动描述',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 3,
        placeholder: '请输入活动描述',
      },
    },
    {
      fieldName: 'content',
      label: '活动详情',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 5,
        placeholder: '请输入活动详情',
      },
    },
    {
      fieldName: 'venueId',
      label: '活动阵地',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择活动阵地',
        api: getVenueInfoOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'address',
      label: '活动地址',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入活动地址',
      },
    },
    {
      fieldName: 'longitude',
      label: '经度',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入经度',
        precision: 6,
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'latitude',
      label: '纬度',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入纬度',
        precision: 6,
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'startTime',
      label: '开始时间',
      component: 'DatePicker',
      formItemClass: 'col-span-1',
      componentProps: {
        type: 'datetime',
        placeholder: '请选择开始时间',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
      },
    },
    {
      fieldName: 'endTime',
      label: '结束时间',
      component: 'DatePicker',
      formItemClass: 'col-span-1',
      componentProps: {
        type: 'datetime',
        placeholder: '请选择结束时间',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
      },
    },
    {
      fieldName: 'signupStartTime',
      label: '报名开始时间',
      component: 'DatePicker',
      formItemClass: 'col-span-1',
      componentProps: {
        type: 'datetime',
        placeholder: '请选择报名开始时间',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
      },
    },
    {
      fieldName: 'signupEndTime',
      label: '报名截止时间',
      component: 'DatePicker',
      formItemClass: 'col-span-1',
      componentProps: {
        type: 'datetime',
        placeholder: '请选择报名截止时间',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
      },
    },
    {
      fieldName: 'maxParticipants',
      label: '最大参与人数',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '0表示不限',
        min: 0,
        controlsPosition: 'right',
        class: '!w-full',
      },
      defaultValue: 0,
    },
    {
      fieldName: 'pointReward',
      label: '积分奖励',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入积分奖励',
        min: 0,
        controlsPosition: 'right',
        class: '!w-full',
      },
      defaultValue: 0,
    },
    {
      fieldName: 'experienceReward',
      label: '经验奖励',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入经验奖励',
        min: 0,
        controlsPosition: 'right',
        class: '!w-full',
      },
      defaultValue: 0,
    },
    {
      fieldName: 'serviceHours',
      label: '服务时长(小时)',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入服务时长',
        min: 0,
        step: 0.5,
        controlsPosition: 'right',
        class: '!w-full',
      },
      defaultValue: 0,
    },
    {
      fieldName: 'organizer',
      label: '主办方',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入主办方',
      },
    },
    {
      fieldName: 'contactPerson',
      label: '联系人',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入联系人',
      },
    },
    {
      fieldName: 'contactPhone',
      label: '联系电话',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入联系电话',
      },
    },
    {
      fieldName: 'attachments',
      label: '附件',
      component: 'FileUpload',
      formItemClass: 'col-span-2',
      componentProps: {
        maxCount: 5,
      },
    },
    {
      fieldName: 'activityStatus',
      label: '活动状态',
      component: 'Select',
      formItemClass: 'col-span-1',
      componentProps: {
        options: ACTIVITY_STATUS_OPTIONS,
        placeholder: '请选择活动状态',
      },
      defaultValue: 0,
    },
    {
      fieldName: 'isRecommend',
      label: '是否推荐',
      component: 'RadioGroup',
      formItemClass: 'col-span-1',
      componentProps: {
        options: [
          { label: '否', value: 0 },
          { label: '是', value: 1 },
        ],
      },
      defaultValue: 0,
    },
    {
      fieldName: 'isHot',
      label: '是否热门',
      component: 'RadioGroup',
      formItemClass: 'col-span-1',
      componentProps: {
        options: [
          { label: '否', value: 0 },
          { label: '是', value: 1 },
        ],
      },
      defaultValue: 0,
    },
    {
      fieldName: 'sort',
      label: '排序',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入排序',
        controlsPosition: 'right',
        class: '!w-full',
      },
      defaultValue: 0,
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'title',
      label: '活动标题',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入活动标题',
      },
    },
    {
      fieldName: 'categoryId',
      label: '活动分类',
      component: 'InputNumber',
      componentProps: {
        clearable: true,
        placeholder: '请选择活动分类',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'activityStatus',
      label: '活动状态',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: ACTIVITY_STATUS_OPTIONS,
        placeholder: '请选择活动状态',
      },
    },
    {
      fieldName: 'isRecommend',
      label: '是否推荐',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: [
          { label: '否', value: 0 },
          { label: '是', value: 1 },
        ],
        placeholder: '请选择',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        clearable: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<CmtActivityInfoApi.ActivityInfo>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '活动ID',
      minWidth: 120,
    },
    {
      field: 'title',
      title: '活动标题',
      minWidth: 200,
    },
    {
      field: 'coverImage',
      title: '封面图片',
      minWidth: 100,
      cellRender: {
        name: 'CellImage',
        props: { size: 60 },
      },
    },
    {
      field: 'categoryId',
      title: '活动分类',
      minWidth: 120,
    },
    {
      field: 'activityStatus',
      title: '活动状态',
      minWidth: 100,
      slots: { default: 'activityStatus' },
    },
    {
      field: 'startTime',
      title: '开始时间',
      minWidth: 150,
      formatter: 'formatDateTime',
    },
    {
      field: 'endTime',
      title: '结束时间',
      minWidth: 150,
      formatter: 'formatDateTime',
    },
    {
      field: 'maxParticipants',
      title: '最大人数',
      minWidth: 100,
    },
    {
      field: 'currentParticipants',
      title: '当前报名',
      minWidth: 100,
    },
    {
      field: 'isRecommend',
      title: '推荐',
      minWidth: 80,
      slots: { default: 'isRecommend' },
    },
    {
      field: 'isHot',
      title: '热门',
      minWidth: 80,
      slots: { default: 'isHot' },
    },
    {
      field: 'viewCount',
      title: '浏览次数',
      minWidth: 100,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 150,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
